﻿using Dapper;
using DBOptionPro.DBUtils;
using DBOptionPro.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DBOptionPro.DAL
{
    /// <summary>
    /// DBF数据库访问类
    /// </summary>
    public class MyData_DBFDAL : DBHelper<MyData>
    {
        public MyData_DBFDAL(DBFFileInfo file) : base(file.FilePath)
        {


        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="table">表名(要从哪个表查数据)</param>
        /// <param name="field">字段名(要取数据的字段名)</param>
        /// <param name="where">查询条件，sql条件拼接(这个是查询条件，例如  字段1=@feid1 and 字段2=@feild2)</param>
        /// <param name="par">条件参数(new { 参数1 = 值1, 参数2 = 值 2}),注意这个参数1、参数2的名称与上面@feid1、@feid2要对应</param>
        /// <returns></returns>
        private T Getfv<T>(string table, string field, string where, object par)
        {
            try
            {
                if (_DBConnection.State != ConnectionState.Open)
                {
                    _DBConnection.Open();
                }
                string sql = $"select {field} from {table} where {where}";

                return _DBConnection.ExecuteScalar<T>(sql, par);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return default;
            }


        }

        public List<MyData> GetDBFDataList()
        {
            List<MyData> datas = new List<MyData>();
            string sql = "select KSH,XM from t_bmk";
            DataTable dtf = base.GetDataTable(sql);
            foreach (DataRow row in dtf.Rows)
            {
                MyData myData = new MyData()
                {
                    ksh = row["KSH"].ToString(),
                    xm = row["XM"].ToString().Trim(),
                };
                datas.Add(myData);
            }
            return datas;
        }


    }
}
